package com.gem.controller.mobile;

import com.alibaba.druid.util.StringUtils;
import com.gem.domain.*;
import com.gem.service.CampusConnService;
import com.gem.service.CampusService;
import com.gem.service.CoreService;
import com.gem.service.UserService;
import com.gem.util.ExcelUtil;
import com.gem.util.MessageUtil;
import com.gem.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import static com.gem.util.ExcelUtil.setHSSFValidation;

/**
 * @Author: 玲
 * @Description: 我的院校控制器（手机端）
 * @create 2019-10-18 14:11
 * @Modified By:
 */
@Controller
@SuppressWarnings("all")
public class MobileCampusController {
    @Autowired
    private CampusService service;

    @Autowired
    private CampusConnService connService;

    @Autowired
    private CoreService coreService;

    @Autowired
    private UserService userService;

    @RequestMapping("/mcampus/findAll")
    public String findAll(Model model,HttpSession session,
                          @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                          @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            User user = (User) session.getAttribute("user");
            List<User> users = userService.findMmembers();
            model.addAttribute("users", users);
            Campus campus = new Campus();
            campus.setPersonname(user.getPersonname());
            PageInfo<Campus> pageBeans = null;
            if (user.getAuthority() == 1){
                pageBeans = service.findAll(currentPage, pageSize);
            }else {
                pageBeans = service.search(campus, currentPage, pageSize);
            }
            if (pageBeans != null && pageBeans.getList().size() > 0) {
                System.out.println(pageBeans.getList());
                model.addAttribute("pageBean", pageBeans);
                List<Integer> pageValue = PageUtil.getPageValue(pageBeans.getPages(), currentPage);
                model.addAttribute("pageValue", pageValue);
            } else {
                model.addAttribute("info", "没有数据请先添加！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/", "服务器异常！请联系管理员！", model);
            return "mobile_message";
        }
        return "mobile_campus/campus";
    }
    @RequestMapping("/mcampus/saveUI")
    public String saveUI(Model model) {
        try {
            List<User> users = userService.findMmembers();
            model.addAttribute("users", users);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/mcampus/infoFindAll", "服务器异常！请联系管理员！", model);
            return "mobile_message";
        }
        return "mobile_campus/campusInsert";
    }

    @RequestMapping("/mcampus/save")
    public String save(Campus campus, CampusInfo info, Model model, HttpSession session) {
        try {
            User user = (User) session.getAttribute("user");
            service.save(campus, info);
            if (user.getAuthority() == 1) {
                MessageUtil.returnInfoPage("3", "/mcampus/infoFindAll", "添加成功！", model);
            } else {
                MessageUtil.returnInfoPage("3", "/mcampus/search?personname=" + user.getPersonname(), "添加成功！", model);
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/mcampus/infoFindAll", "服务器异常！请联系管理员！", model);
        }
        return "mobile_message";
    }

    @RequestMapping("/mcampus/campusInfo")
    public String campusInfo(Integer campusId, Model model,
                             @RequestParam("RcurrentPage") int RcurrentPage,
                             @RequestParam("RpageSize") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            CampusInfo info = service.findInfo(campusId);
            model.addAttribute("CampusInfo", info);
            model.addAttribute("campusId", campusId);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/mcampus/infoFindAll", "服务器异常！请联系管理员！", model);
            return "mobile_message";
        }
        return "mobile_campus/campusInfo";
    }

    @RequestMapping("/mcampus/infoUpdateUI")
    public String infoUpdateUI(Integer campusId, Model model,
                               @RequestParam("RcurrentPage") int RcurrentPage,
                               @RequestParam("RpageSize") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            CampusInfo info = service.findInfo(campusId);
            model.addAttribute("CampusInfo", info);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/mcampus/infoFindAll", "服务器异常！请联系管理员！", model);
            return "mobile_message";
        }
        return "mobile_campus/campusInfoUpdate";
    }

    @RequestMapping("/mcampus/infoUpdate")
    public String infoUpdate(CampusInfo info, Model model,
                             @RequestParam("RcurrentPage") int RcurrentPage,
                             @RequestParam("RpageSize") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            if (service.updateInfo(info) == 1) {
                MessageUtil.returnInfoPage("3", "/mcampus/campusInfo?campusId=" + info.getCampusId()
                        + "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize, "修改成功！", model);
            } else {
                MessageUtil.returnInfoPage("3", "/mcampus/campusInfo?campusId=" + info.getCampusId()
                        + "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize, "未作修改！", model);
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/mcampus/campusInfo?campusId=" + info.getCampusId()
                    + "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize, "服务器异常！请联系管理员！", model);

        }
        return "mobile_message";
//        return "redirect:/campus/campusInfo?campusId=" + info.getCampusId() + "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize;
    }


    @RequestMapping("/mcampus/infoSaveUI")
    public String infoSaveUI(Integer campusId, Model model,
                             @RequestParam("RcurrentPage") int RcurrentPage,
                             @RequestParam("RpageSize") int RpageSize) {
        model.addAttribute("RcurrentPage", RcurrentPage);
        model.addAttribute("RpageSize", RpageSize);
        try {
            Campus campus = service.findById(campusId);
            model.addAttribute("campus", campus);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/mcampus/infoFindAll", "服务器异常！请联系管理员！", model);
            return "mobile_message";
        }
        return "mobile_campus/campusInfoInsert";
    }

    @RequestMapping("/mcampus/infoSave")
    public String infoSave(CampusInfo info, Model model,
                           @RequestParam("RcurrentPage") int RcurrentPage,
                           @RequestParam("RpageSize") int RpageSize) {
        try {
            service.saveInfo(info);
            MessageUtil.returnInfoPage("3", "/mcampus/campusInfo?campusId="
                    + info.getCampusId() + "&RcurrentPage=" + RcurrentPage + "&RpageSize=" + RpageSize, "保存成功！", model);
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/mcampus/infoFindAll", "服务器异常！请联系管理员！", model);

        }
        return "mobile_message";
//        return "redirect:/campus/campusInfo?campusId=" + info.getCampusId();
    }

    @RequestMapping("/mcampus/search")
    public String search(Model model, Campus campus,HttpSession session,
                         @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                         @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            User user = (User) session.getAttribute("user");
            campus.setPersonname(user.getPersonname());
            String campusName = campus.getCampusName();
            String personname = campus.getPersonname();
            PageInfo<Campus> pageBeans = service.search(campus, currentPage, pageSize);
            if (pageBeans.getList() != null && pageBeans.getList().size() > 0) {
                model.addAttribute("pageBean", pageBeans);
                List<Integer> pageValue = PageUtil.getPageValue(pageBeans.getPages(), currentPage);
                model.addAttribute("pageValue", pageValue);
                model.addAttribute("searchUrl", "/mcampus/search");
                model.addAttribute("campusName", campusName);
                model.addAttribute("personname", personname);
            } else {
                model.addAttribute("info", "没有数据请先添加！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.returnInfoPage("3", "/mcampus/findAll", "服务器异常！请联系管理员！", model);
            return "mobile_message";
        }
        return "mobile_campus/campus";
    }
}
